package jp.co.neosystem.s2wg.service.flex.impl;

import java.util.List;

import jp.co.neosystem.s2wg.dto.flex.ArgsLogin;
import jp.co.neosystem.s2wg.dto.flex.ResultLogin;
import jp.co.neosystem.s2wg.dto.flex.User;
import jp.co.neosystem.s2wg.entity.TblMstAccount;
import jp.co.neosystem.s2wg.service.dao.AccountService;
import jp.co.neosystem.s2wg.service.dao.DepartmentService;
import jp.co.neosystem.s2wg.service.flex.LoginService;
import jp.co.neosystem.s2wg.util.Logger;
import jp.co.neosystem.s2wg.util.MessageCode;
import jp.co.neosystem.s2wg.util.StatusFactory;

import org.seasar.flex2.rpc.remoting.service.annotation.RemotingService;
import org.seasar.framework.beans.util.BeanMap;

/**
 * ログイン処理サービスクラス。
 */
@RemotingService
public class LoginServiceImpl implements LoginService {

	public AccountService accountService;

	public DepartmentService departmentService;

	@Override
	public ResultLogin login(final ArgsLogin args, final boolean isTest) {
		final ResultLogin ret = new ResultLogin();
		ret.setStatus(StatusFactory.getStatus(MessageCode.LOGIN_OK));
		final BeanMap condition = new BeanMap();
		condition.put("loginId", args.getAccount());
		condition.put("password", args.getPassword());

		final List<TblMstAccount> account = accountService
				.findByCondition(condition);

		if (account == null || account.size() == 0) {
			ret.setStatus(StatusFactory.getStatus(MessageCode.LOGIN_NG));
			return ret;
		}
		final User user = new User();
		user.setId(account.get(0).userId);
		user.setAccount(account.get(0).loginId);
		user.setViewName(account.get(0).accountName);
		user.setSectionName(departmentService.getDepartmentName(account.get(0).departmentId));
		ret.setUser(user);

		Logger.debug(ret);
		return ret;
	}

}
